<template>
	<div>
		<el-upload
			action="#"
			:class="{'hideUploadButton':fileList.length >= maxCount}"
			:data="extraParameters"
			:list-type="listType"
			:file-list="fileList"
			:before-upload="beforeUpload"
			:on-remove="handleRemove"
			:on-preview="handlePreview"
			:limit="maxCount"
			:on-exceed="handleExceed"
			:show-file-list="showFile"
			:http-request="uploadImage"
			:on-success="handleUploadSuccess"
		>
		<i class="el-icon-plus"></i>
		</el-upload>
		<el-dialog :visible.sync="dialogVisible">
			<img width="100%" :src="dialogImageUrl" alt />
		</el-dialog>
	</div>
</template>
<script>
import { uploadImage } from '@/api/common/upload'
export default {
  name: "multiUpload",
  props: {
    //图片属性数组
    value: Array,
    //最大上传图片数量
    maxCount: {
      type: Number,
      default: 30
    },
	//上传文件大小 默认5
	maxSize:{
		type: Number,
		default: 5
	},
    listType:{
      type: String,
      default: "picture-card"
    },
    showFile:{
      type: Boolean,
      default: true
    },
	uploadType: String,
	acceptType:Array,
	uploadSuccessdelete:{
		type:Boolean,
		default: false
	}
  },
  data() {
    return {
		dialogVisible: false,
		dialogImageUrl: null,
		extraParameters:{
			uploadType:this.uploadType
		},
		fileList: [],
    };
  },
  methods: {
    emitInput(fileList) {
      let value = [];
      for (let i = 0; i < fileList.length; i++) {
        value.push(fileList[i].url);
      }
      this.$emit("input", value);
    },
    handleRemove(file, fileList) {
		if(this.fileList.indexOf(file) != -1){
			this.fileList.splice(this.fileList.indexOf(file),1)
		}
		this.emitInput(fileList);
    },
    handlePreview(file) {
      this.dialogVisible = true;
      this.dialogImageUrl = file.url;
    },
    beforeUpload(file) {
		console.log(file)
		const fileType = this.acceptType.indexOf(file.type) != '-1'
		const fileSize = file.size / 1024 / 1024 < this.maxSize;
		if(!fileType){
			this.$message.error('上传文件只能是'+this.acceptType+'格式!');
		}
		if (!fileSize) {
			this.$message.error('上传头像图片大小不能超过'+this.maxSize+'MB!');
		}
		return fileType && fileSize;
    },
    handleExceed(files, fileList) {
		console.log(files,fileList)
		this.$message({
			message: "最多只能上传" + this.maxCount + "张图片",
			type: "warning",
			duration: 1000
		});
    },
	handleUploadSuccess(res,file) {
		this.fileList.push({
			name: file.name,
			url: res.data.responseData[0]
		});
		this.emitInput(this.fileList);
		//如果上传成功后需要删除文件则进行删除操作
		if(this.uploadSuccessdelete){
			this.fileList.splice(this.fileList.indexOf(file),1)
		}
	},
	uploadImage(val){
		console.log(this.extraParameters)
		let formData = new FormData();
		formData.append('uploadType',this.extraParameters.uploadType)
		formData.append('files',val.file)
		uploadImage(formData).then(res =>{
			if(res.data.responseCode == '200'){
				val.onSuccess(res)
			}
		}).catch(err =>{
			this.fileList.splice(this.fileList.indexOf(val.file),1)
			this.$message.error(err.data.responseMessage)
		})
	},
  }
};
</script>
<style>
.hideUploadButton .el-upload--picture-card{
  display:none;   /* 上传按钮隐藏 */
}	
</style>


